﻿#include <gtest/gtest.h>
#include <stdint.h> 
#include "HiUnSafeSmartHeapList.h"

static const  int SIZE = 1920 * 1080 * 3 + 32;
TEST(HiUnSafeSmartHeapList, init){
	int releaseIndex = 0;
	HiUnSafeSmartHeapList* list = new HiUnSafeSmartHeapList();
	list->Init(100, [&](){
		return new uint8_t[SIZE];
	}, [&](void* ptr){
		if (nullptr == ptr) {
			return;
		}
		uint8_t* arr = (uint8_t*)ptr;
		releaseIndex++;
	});
	delete list;
	list = nullptr;
	ASSERT_EQ(100, releaseIndex);
}

TEST(HiUnSafeSmartHeapList, create){
	int releaseIndex = 0;
	HiUnSafeSmartHeapList* list = new HiUnSafeSmartHeapList();
	list->Init(100, [&](){
		return new uint8_t[SIZE];
	}, [&](void* ptr){
		if (nullptr == ptr) {
			return;
		}
		uint8_t* arr = (uint8_t*)ptr;
		releaseIndex++;
	});
	void* p1 = list->CreateData();
	ASSERT_NE(nullptr, p1);
	
	list->CreateData();
	int usingCount = list->UsingCount();
	ASSERT_EQ(2, usingCount);
	delete list;
	list = nullptr;
	ASSERT_EQ(100, releaseIndex);
}

TEST(HiUnSafeSmartHeapList, destoryAndUsingCount){
	int releaseIndex = 0;
	HiUnSafeSmartHeapList* list = new HiUnSafeSmartHeapList();
	list->Init(100, [&](){
		return new uint8_t[SIZE];
	}, [&](void* ptr){
		if (nullptr == ptr) {
			return;
		}
		uint8_t* arr = (uint8_t*)ptr;
		releaseIndex++;
	});
	void* p1 = list->CreateData();
	ASSERT_NE(nullptr, p1);
	
	list->CreateData();
	int usingCount = list->UsingCount();
	ASSERT_EQ(2, usingCount);
	
	list->Destory(p1);
	
	usingCount = list->UsingCount();
	ASSERT_EQ(1, usingCount);
	delete list;
	list = nullptr;
	ASSERT_EQ(100, releaseIndex);
}


